home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-07-28 | 5.6 KB | 241 lines |
- /*
- * Info.java - Provides an info button
- * eCross is Copyright (C) 2000 Romain Guy
- * guy.romain@bigfoot.com
- * www.jext.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
- import waba.fx.*;
- import waba.io.*;
-
- /**
- * A control that displays an info button
- * @version 1.0
- * @author Romain Guy <guy.romain@bigfoot.com>
- */
-
- public class Info
- {
- private byte state = 0;
- private byte page, maxPages;
- private boolean lGray, rGray;
-
- private Catalog infos;
-
- // info
- private boolean inverted = false;
- private Image iImage = new Image("datas/info.bmp");
- private Image niImage = new Image("datas/ninfo.bmp");
-
- // arrows
- private Image leftArrow = new Image("datas/larrow.bmp");
- private Image leftGrayedArrow = new Image("datas/glarrow.bmp");
- private Image rightArrow = new Image("datas/rarrow.bmp");
- private Image rightGrayedArrow = new Image("datas/grarrow.bmp");
-
- /**
- * Creates a new info button.
- */
-
- public Info()
- {
- }
-
- /**
- * Handles a pen down event.
- * @param parent The caller
- * @param g The <code>Graphics</code> surface of the caller
- * @param x The x coordinate of the event
- * @param y The y coordinate of the event
- * @return <code>true</code> if the event was handled
- */
-
- public boolean handlePenEvent(eCross parent, Graphics g, int x, int y)
- {
- // pages flipping
- if (state == 1)
- {
- if (x > 13 && x < 43 && y > 121 && y < 136)
- {
-
- state = 0;
- infos.close();
- parent.draw();
- parent.resumeTimer();
-
- } else if (!lGray && x > 128 && x < 134 && y > 125 && y < 136) {
-
- if (--page == 0)
- {
- g.drawImage(leftGrayedArrow, 128, 125);
- lGray = true;
- }
-
- if (rGray)
- {
- g.drawImage(rightArrow, 140, 125);
- rGray = false;
- }
-
- paintPage(g);
-
- } else if (!rGray && x > 140 && x < 146 && y > 125 && y < 136) {
-
- if (++page == maxPages - 1)
- {
- g.drawImage(rightGrayedArrow, 140, 125);
- rGray = true;
- }
-
- if (lGray)
- {
- g.drawImage(leftArrow, 128, 125);
- lGray = false;
- }
-
- paintPage(g);
- }
-
- return true;
- }
-
- if (x > 160 - (10 + 1) && x < 160 - 1 && y > 2 && y < 12)
- {
- // the info icon was tapped
- inverted = true;
- g.drawImage(niImage, 160 - (10 + 1), 2);
- return true;
- }
-
- return false;
- }
-
- public boolean handlePenUpEvent(Graphics g, int x, int y)
- {
- if (inverted)
- {
- inverted = false;
- g.drawImage(iImage, 160 - (10 + 1), 2);
-
- return true; // (x > 160 - (10 + 1) && x < 160 - 1 && y > 2 && y < 12);
- }
-
- return false;
- }
-
- /**
- * Paints infos.
- */
-
- public void showInfo(eCross parent, Graphics g)
- {
- parent.pauseTimer();
-
- state = 1;
- page = 0;
- lGray = true;
- rGray = false;
-
- g.setColor(255, 255, 255);
- // erase place of the window
- g.fillRect(9, 19, 142, 122);
-
- g.setColor(0, 0, 0);
- g.drawRect(10, 20, 140, 120);
-
- Font font = new Font("Helvetica", Font.BOLD, 10);
- FontMetrics fm = parent.getFontMetrics(font);
- g.setFont(font);
-
- g.drawRect(13, 121, 30, 15);
- g.drawDots(15, 136, 43, 136);
- g.drawDots(43, 135, 43, 123);
- String text = new String("Ok");
- g.drawText(text, 13 + (30 - fm.getTextWidth(text)) / 2, 123);
-
- g.drawImage(leftGrayedArrow, 128, 125);
- g.drawImage(rightArrow, 140, 125);
-
- infos = new Catalog("eCross Infos.eCrs.eCrs", Catalog.READ_ONLY);
- if (!infos.isOpen())
- return;
- infos.setRecordPos(0);
- byte[] b = new byte[1];
- infos.readBytes(b, 0, 1);
- maxPages = b[0];
-
- g.setFont(new Font("Helvetica", Font.PLAIN, 10));
- paintPage(g);
- }
-
- // paints current informations page
-
- private void paintPage(Graphics g)
- {
- if (!infos.setRecordPos(page + 1))
- return;
-
- g.setColor(255, 255, 255);
- g.fillRect(11, 21, 138, 100);
-
- g.setColor(0, 0, 0);
-
- int last = -1, count = 0;
-
- byte[] b = new byte[infos.getRecordSize()];
- infos.readBytes(b, 0, b.length);
-
- char[] _page = new char[b.length];
- for (int i = 0; i < b.length; i++)
- _page[i] = (char) b[i];
- b = null;
-
- int len = _page.length;
-
- for (int i = 0; i < len; i++)
- {
- if (_page[i] == '\n')
- {
- g.drawText(new String(_page, ++last, i - last), 13, 21 + count * 10 + (count + 1));
- count++;
- last = i;
- }
- }
-
- _page = null;
- g.drawText((page + 1) + "/" + maxPages, 75, 123);
- }
-
- public void paint(Graphics g)
- {
- g.drawImage(iImage, 160 - (10 + 1), 2);
- }
-
- public void free()
- {
- iImage.free();
- niImage.free();
-
- leftArrow.free();
- leftGrayedArrow.free();
- rightArrow.free();
- rightGrayedArrow.free();
- }
- }
-
- // End of Info.java
-